﻿--use PolePosition
GO

IF EXISTS (SELECT *  FROM sys.objects  WHERE object_id = OBJECT_ID(N'fn_VendorDistributionPole_GetNoteString') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
	DROP FUNCTION fn_VendorDistributionPole_GetNoteString

GO
CREATE FUNCTION fn_VendorDistributionPole_GetNoteString
(
	 @DistributionPoleId int
)
RETURNS varchar(MAX)
AS
BEGIN

	DECLARE @Join varchar(MAX)
	SET @Join = ''
	
	SELECT 
		@Join = @Join + Note.[Name] + ', '
	FROM tb_Vendor_Distribution_Results Result
	INNER JOIN tb_Vendor_InspVisit_XmissionStructures_Notes Note
		ON Note.Vendor_InspVisit_XmissionStructures_NoteId = Result.Vendor_Distribution_ResultTypeFk
	WHERE Result.Vendor_Distribution_PoleFk = @DistributionPoleId

	IF (@Join <> '' AND NOT @Join IS NULL)
		SET @Join = LEFT(@Join, LEN(@Join) - 1) 

	RETURN @Join

END

GO
